module EmailHelpers
  require 'mail'

  def send_mail(message)
    Mail.defaults do
      delivery_method :smtp,
                      address: CONFIG[:email][:address],
                      port: CONFIG[:email][:port],
                      domain: CONFIG[:email][:helo],
                      user_name: CONFIG[:email][:user],
                      password: CONFIG[:email][:secret],
                      authentication: :plain,
                      openssl_verify_mode: 'none',
                      enable_starttls_auto: true
    end

    mail = Mail.read_from_string(message.to_s)

    begin
      mail.deliver!
    rescue Exception => e
      puts "fail to send register email #{e.to_s}"
    end
  end

  # @param [Object] _user
  def send_register_email(_user)
    active_url = request.base_url

    message = <<MESSAGE_END
From: #{CONFIG[:email][:from]}
To: #{_user.email}
MIME-Version: 1.0
Content-type: text/html;charset=utf-8
Subject: kcoin 帐号激活

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <html>
    <head>
        <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
        <style media="all" type="text/css">
            td, p, h1, h3, a {
                font-family: Microsoft YaHei, sans-serif;
            }
        </style>
    </head>

<body LINK="#c6d4df" ALINK="#c6d4df" VLINK="#c6d4df" TEXT="#c6d4df"
      style="font-family: Microsoft YaHei, sans-serif; font-size: 14px; color: #c6d4df;">
<table style="width: 600px; background-color: #393836;" align="center" cellspacing="0" cellpadding="0">
    <tr>
        <td style=" height: 65px; background-color: #000000; border-bottom: 1px solid #4d4b48;">
            <a style="text-decoration: none !important;" href="http://kcoin.kaiyuanshe.cn/">
            <span style="font-size: 36px;color: #66c0f4;font-family: Microsoft YaHei, sans-serif;font-weight: bold;padding-left: 10px;padding-left: 10px;">
                <span style="color: #00a3d9;">K</span>coin
            </span>
            </a>
        </td>
    </tr>
    <tr>
        <td bgcolor="#17212e">
            <table width="470" border="0" align="center" cellpadding="0" cellspacing="0"
                   style="padding-left: 5px; padding-right: 5px; padding-bottom: 10px;">

                <tr bgcolor="#17212e">
                    <td style="padding-top: 32px;">
					<span style="padding-top: 16px; padding-bottom: 16px; font-size: 24px; color: #66c0f4; font-family: Microsoft YaHei, sans-serif; font-weight: bold;">
						尊敬的 #{_user.login}：
					</span><br>
                    </td>
                </tr>

                <tr>
                    <td style="padding-top: 12px;">
					<span style="font-size: 17px; color: #c6d4df; font-family: Microsoft YaHei, sans-serif; font-weight: bold;">
						<p>您在 kcoin 上注册了一个新用户，帐号为：#{_user.login}</p>
                        <p>请点下面链接访问官网：</p>
                        <p><a style="color: #8f98a0;"
                              href="#{active_url}">#{active_url}</a>
                        </p>
					</span>
                    </td>
                </tr>


                <tr>
                    <td>
                        <br>
                    </td>
                </tr>


                <tr bgcolor="#121a25">
                    <td style="padding: 20px; font-size: 12px; line-height: 17px; color: #c6d4df; font-family: Microsoft YaHei, sans-serif;">
                        Kcion 是一个基于区块链的社区贡献激励
                    </td>

                </tr>
            </table>
        </td>
    </tr>

    <td bgcolor="#000000">
        <table width="460" height="55" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr valign="top">
                <td width="350" valign="top">
                    <span style="color: #999999; font-size: 12px; font-family: Microsoft YaHei, sans-serif;">本邮件由系统发出，请勿直接回复。</span>
                </td>
            </tr>
        </table>
    </td>
    </tr>
</table>

</body>
</html>


MESSAGE_END

    send_mail(message)


  end

  def send_project_import_email(project, user,mail)
    action_url = request.base_url + '/project'

    message = <<MESSAGE_END
From: #{CONFIG[:email][:from]}
To: #{mail}
MIME-Version: 1.0
Content-type: text/html;charset=utf-8
Subject: KCoin 项目导入提醒

<!DOCTYPE html>
<html lang="en">

<head>
    <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
    <style media="all" type="text/css">
        * {
            font-family: Microsoft YaHei, sans-serif;
        }

        .main {
            width: 550px;
            height: 780px;
            margin: auto;
            background: url();
            text-align: center;
        }

        .title {
            font-size: 40px;
            color: #ffffff;
            margin: auto;
            top: 105px;
            position: relative;
        }

        .img {
            background: #737373;
            width: 80px;
            height: 80px;
            border-radius: 100%;
            position: relative;
            top: 148px;
            margin: auto;
        }

        .content {
            font-size: 16px;
            color: #737373 !important;
            text-align: left;
            top: 210px;
            position: relative;
            margin: auto;
            width: 420px;
        }

        .btn {
            background: #ff5c5d;
            border-radius: 10px;
            width: 220px;
            height: 60px;
            font-size: 20px;
            color: #ffffff;
            position: relative;
            top: 294px;
            left: 165px;
            display: block;
            text-decoration: none !important;
            text-align: center;
            line-height: 60px;
        }
    </style>
</head>

<body>

<div class="main">
    <div class="title">项目导入通知</div>
    <img class="img" src="#{project[:importer_avatar_url]}"/>
    <div class="content">#{project['importer_user']} 已在KCoin平台导入你们的项目—— #{project[:custom_name]}，KCoin平台是一个基于区块链激励开源贡献的经济系统。您可通过下面的链接前往平台查看项目。</div>
    <a class="btn" href="#{action_url}" >查看项目</a>
</div>

</body>
</html>


MESSAGE_END

    send_mail(message)
  end
end
